package ace.module.oauth2.server.core.impl.manager.impl;

import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.manager.spring.AbstractSpringDtoManager;
import ace.module.oauth2.server.api.model.dto.Oauth2RegisteredClientDto;
import ace.module.oauth2.server.core.impl.dao.entity.Oauth2RegisteredClient;
import ace.module.oauth2.server.core.impl.manager.Oauth2RegisteredClientManager;
import ace.module.oauth2.server.core.impl.manager.cache.Oauth2RegisteredClientCacheManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
* oauth2 客户端 spring data jdbc querydsl rsql manager implement.
*
* @author Caspar
*/
@Slf4j
@AllArgsConstructor
@Component
public class Oauth2RegisteredClientManagerImpl
    extends AbstractSpringDtoManager<Oauth2RegisteredClient, String, Oauth2RegisteredClientDto>
    implements Oauth2RegisteredClientManager {
  private final Oauth2RegisteredClientCacheManager oAuth2RegisteredClientCacheManager;

  @Transactional(rollbackFor = Exception.class)
  @Override
  public String save(Oauth2RegisteredClient oAuth2RegisteredClient) {
    this.save(oAuth2RegisteredClient, true);
    this.oAuth2RegisteredClientCacheManager.clear(oAuth2RegisteredClient);
    return oAuth2RegisteredClient.getId();
  }

  @Override
  public Oauth2RegisteredClientDto getDtoByClientId(String clientId) {
    Oauth2RegisteredClient oAuth2RegisteredClient =
        this.oAuth2RegisteredClientCacheManager.getByClientId(clientId);
    return this.getEntityToDtoConverter().convert(oAuth2RegisteredClient);
  }
}
